<template>
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" size="small" :inline="true">
      <el-form-item label="产品名称" prop="tuzhi">
        <el-input v-model="queryParams.tuzhi" placeholder="请输入产品名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="图号" prop="tuhao">
        <el-input v-model="queryParams.tuhao" placeholder="请输入图号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格型号" prop="guige">
        <el-input v-model="queryParams.guige" placeholder="请输入规格型号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生产批次号" prop="picixuhao">
        <el-input v-model="queryParams.picixuhao" placeholder="请输入生产批次号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-row :gutter="10" class="mb8">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handlePrint">打印</el-button>
          <el-button type="primary" @click="handleExportExcel">导出EXCEL</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="productInProcessList" :stripe="true" :show-overflow-tooltip="true" border>
      <el-table-column label="序号" align="center" prop="id" sortable />
      <el-table-column label="产品名称" align="center" prop="name" />
      <el-table-column label="图号" align="center" prop="tuhao" />
      <el-table-column label="规格型号" align="center" prop="guige" />
      <el-table-column label="合同类型" align="center" prop="hetong_leixing" />
      <el-table-column label="合同订单编号" align="center" prop="dingdan" />
      <el-table-column label="生产批次号" align="center" prop="picihao" />
      <el-table-column label="有无三维" align="center" prop="sanwei" />
      <el-table-column label="材料" align="center" prop="cailiao" />
      <el-table-column label="下料尺寸" align="center" prop="xialiao" />
      <el-table-column label="计划数量" align="center" prop="num_jihua" />
      <el-table-column label="实投数量" align="center" prop="num_touchan">
      </el-table-column>
      <el-table-column label="合格数量" align="center" prop="num_hege">
      </el-table-column>
      <el-table-column label="客户要求交期" align="center" prop="jiaoqi">
      </el-table-column>
      <el-table-column label="下计划日期" align="center" prop="riqi_jihua">
      </el-table-column>
      <el-table-column label="剩余时间" align="center" prop="shengyu">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="beizhu">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" sortable>
      </el-table-column>
      <el-table-column label="录入时间" align="center" prop="stime">
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import * as XLSX from 'xlsx'
import { getTuzhiZhong } from '@/api/productManage/index'

export default {
  name: 'SystemUser',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        tuzhi: "",
        tuhao: "",
        guige: "",
        picixuhao: ""
      },
      productInProcessList: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = false
      getTuzhiZhong(this.queryParams).then((response) => {
        this.productInProcessList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handlePrint() {
      window.print()
    },

    /** 导出 Excel 功能 */
    handleExportExcel() {
      if (this.productInProcessList.length === 0) {
        this.$message.warning('没有数据可供导出')
        return
      }
      // 更新后的 prop 到中文表头的映射
      const headerMap = {
        id: '序号',
        name: '产品名称',
        tuhao: '图号',
        guige: '规格型号',
        hetong_leixing: '合同类型',
        dingdan: '合同订单编号',
        picihao: '生产批次号',
        sanwei: '有无三维',
        cailiao: '材料',
        xialiao: '下料尺寸',
        num_jihua: '计划数量',
        num_touchan: '实投数量',
        num_hege: '合格数量',
        jiaoqi: '客户要求交期',
        riqi_jihua: '下计划日期',
        shengyu: '剩余时间',
        beizhu: '备注',
        state: '状态',
        stime: '录入时间'
      }

      // 转换数据以使用中文表头
      const formattedData = this.productInProcessList.map(item => {
        const newItem = {}
        for (const key in item) {
          if (headerMap[key]) {
            newItem[headerMap[key]] = item[key]
          }
        }
        return newItem
      })

      const worksheet = XLSX.utils.json_to_sheet(formattedData)
      const workbook = XLSX.utils.book_new()
      XLSX.utils.book_append_sheet(workbook, worksheet, '加工中列表')
      XLSX.writeFile(workbook, '加工中列表.xls')
    }
  }
}
</script>
